package com.zsix.system.service.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zsix.system.mapper.taskAssigneeMapper.AssigneeUserMapper;
import com.zsix.common.entity.taskAssignee.AssigneeDSREntity;
import com.zsix.common.entity.taskAssignee.AssigneeUserEntity;
import com.zsix.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Slf4j
@Component
@RequiredArgsConstructor
public class ProcessDefManager {
    @Autowired
    private AssigneeUserMapper assigneeUserMapper;

    /**
     * 删除节点接收人
     * @param usertaskid
     * @return
     */
    public String deleteAssigneeUserByTaskid(String usertaskid){
        assigneeUserMapper.deleteAssigneeUserByTaskid(usertaskid);
        return "执行成功";
    }

    /**
     *配置某个任务节点处理人
     * @param assigneeList
     * @param usertaskid
     * @return
     */
    public String setAssigneeUser(String assigneeList, String usertaskid,String ruleName){
        JSONArray userlist = JSONObject.parseArray(assigneeList);
        List<AssigneeUserEntity> assigneeUserEntityList = Optional.ofNullable(userlist)
                .map(list->list.stream().map(it->{
                    JSONObject json = JSONObject.parseObject(JSON.toJSONString(it));
                    AssigneeUserEntity assigneeUserEntity = new AssigneeUserEntity();
                    if(StringUtils.isNull(json.getString("username"))){
                        assigneeUserEntity.setUsername(json.getString("id"));
                    }
                    else {
                        assigneeUserEntity.setUsername(json.getString("username"));
                    }
                    assigneeUserEntity.setUsertaskid(usertaskid);
                    assigneeUserEntity.setRuleName(ruleName);
                    assigneeUserEntity.setId(UUID.randomUUID().toString());
                    return assigneeUserEntity;
                }).collect(Collectors.toList())).orElse(new ArrayList<>());
        assigneeUserMapper.setAssigneeUser(assigneeUserEntityList);

        return "执行成功";
    }
    /**
     * 设置部门与岗位or角色交集
     * @param assigneeList
     * @param usertaskid
     * @return
     */
    public String setAssigneeByDeptAndStionOrRole(String assigneeList, String usertaskid){
        JSONArray userlist = JSONObject.parseArray(assigneeList);
        List<AssigneeDSREntity> assigneeDSREntities = Optional.ofNullable(userlist)
                .map(list-> list.stream().map(it->{
                            JSONObject json = JSONObject.parseObject(JSON.toJSONString(it));
                            AssigneeDSREntity assigneeDSREntity = new AssigneeDSREntity();
                            assigneeDSREntity.setDsr(json.getString("dsr"));
                            if(!StringUtils.isEmpty(json.getString("targetid")))
                                assigneeDSREntity.setTargetId(json.getString("targetid"));
                            assigneeDSREntity.setUsertaskid(usertaskid);
                            assigneeDSREntity.setId(UUID.randomUUID().toString());
                            return assigneeDSREntity;
                        })
                        .collect(Collectors.toList())).orElse(new ArrayList<>());
        assigneeUserMapper.setAssigneeByDeptAndStionOrRole(assigneeDSREntities);
        return "执行成功";
    }
}
